package com.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.dao.UserMapper;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import sun.plugin.liveconnect.SecurityContextHelper;

import java.util.Date;
import java.util.List;

/**
 * @author gali
 * @description:
 * @date 2022/6/20 17:05
 */
@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService  {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public User getUserInfo(String username) {
        return userMapper.getUserInfo(username);
    }

    @Override
    public int registerUser(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userMapper.insertUser(user.getUsername(),user.getPassword(),user.getRole());
    }

    @Override
    public int updateUserPwd(String oldPwd, String newPwd) {
        //获取登录用户信息
       UserDetails  principal=(UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        User userInfo = userMapper.getUserInfo(username);
        if (passwordEncoder.matches(oldPwd,userInfo.getPassword())){
            return userMapper.updatePwd(username,passwordEncoder.encode(newPwd));
        }
        return 0;
    }
//    @Autowired
//    public JdbcTemplate jdbcTemplate;
//    /**
//     * 增加用户
//     *
//     * @param user
//     */
//    @Override
//    public void addUser(User user) {
//        jdbcTemplate.update("insert  into user(username,birth,age) values (?,?,?) ",
//                user.getUsername(),user.getBirth(),user.getAge());
//    }
//
//    /**
//     * 删除用户
//     *
//     * @param id
//     */
//    @Override
//    public void deleteUser(Long id) {
//        jdbcTemplate.update("delete from user where id =?",id);
//    }
//
//    /**
//     * 修改用户
//     *
//     * @param user
//     */
//    @Override
//    public void updateUser(User user) {
//        jdbcTemplate.update("update user set username=?,birth=?,age=? where id=?",
//                user.getUsername(),user.getBirth(),user.getAge(),user.getId());
//    }
//
//    /**
//     * 查询单个用户
//     *
//     * @param id
//     * @return
//     */
//    @Override
//    public User find(Long id) {
//        return jdbcTemplate.queryForObject("select * from user where id =?",new BeanPropertyRowMapper<>(User.class),id);
//    }
//
//    /**
//     * 查询全部用户
//     *
//     * @return
//     */
//    @Override
//    public List<User> findAll() {
//        System.out.println(jdbcTemplate);
//        return jdbcTemplate.query("select * from user",new BeanPropertyRowMapper<>(User.class));
//    }
}
